<template>
  <div class="app-container compact-container">
    <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入订单名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="截止日期" prop="enddate">
        <el-date-picker clearable
          v-model="queryParams.enddate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择截止日期">
        </el-date-picker>
      </el-form-item>
  
      <el-form-item label="客户QQ" prop="khqq">
        <el-input
          v-model="queryParams.khqq"
          placeholder="请输入客户QQ"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户微信" prop="khwx">
        <el-input
          v-model="queryParams.khwx"
          placeholder="请输入客户微信"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户手机号" prop="khtel">
        <el-input
          v-model="queryParams.khtel"
          placeholder="请输入客户手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单编号" prop="bianhao">
        <el-input
          v-model="queryParams.bianhao"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户旺旺" prop="khww">
        <el-input
          v-model="queryParams.khww"
          placeholder="请输入客户旺旺"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="技术qq" prop="jsqq">
        <el-input
          v-model="queryParams.jsqq"
          placeholder="请输入技术qq"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="技术真实姓名" prop="jszsxm">
        <el-input
          v-model="queryParams.jszsxm"
          placeholder="请输入技术真实姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="技术手机号" prop="jstel">
        <el-input
          v-model="queryParams.jstel"
          placeholder="请输入技术手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单价格" prop="minMoney">
        <el-input
          v-model="queryParams.minMoney"
          placeholder="最低价格"
          clearable
          style="width: 90px"
          @keyup.enter.native="handleQuery"
        />
        <span style="margin: 0 5px;">-</span>
        <el-input
          v-model="queryParams.maxMoney"
          placeholder="最高价格"
          clearable
          style="width: 90px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="包单价格" prop="bdmoney">
        <el-input
          v-model="queryParams.bdmoney"
          placeholder="请输入包单价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
  
      <el-form-item label="退单理由" prop="tddesc">
        <el-input
          v-model="queryParams.tddesc"
          placeholder="请输入退单理由"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="ordersList" @selection-change="handleSelectionChange" size="small">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单名称" align="center" prop="title" />
      <el-table-column label="上传日期" align="center" prop="funddate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.funddate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="加急状态" align="center" prop="host">
        <template slot-scope="scope">
          <el-tag :type="urgentType(scope.row.host)" size="mini" effect="dark">
            {{ urgentFormat(scope.row.host) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="订单状态" align="center" prop="type">
        <template slot-scope="scope">
          <el-tag :type="statusType(scope.row.type)" size="mini">
            {{ statusFormat(scope.row.type) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="截止日期" align="center" prop="enddate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.enddate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="单子简介" align="center" prop="desc" >
          <template slot-scope="scope">
          <el-button type="text" size="mini" @click="showDetail('单子简介', scope.row.desc)">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column label="额外需求" align="center" prop="descs" />
      <el-table-column label="订单价格" align="center" prop="money" />
      <el-table-column label="后续加价" align="center" prop="jmoney" />
      <el-table-column label="客户QQ" align="center" prop="khqq" />
      <el-table-column label="客户微信" align="center" prop="khwx" />
      <el-table-column label="客户手机号" align="center" prop="khtel" />
      <el-table-column label="客户支付宝号" align="center" prop="khzfb" />
      <el-table-column label="订单编号" align="center" prop="bianhao" />
      <el-table-column label="客户旺旺" align="center" prop="khww" />
      <el-table-column label="技术qq" align="center" prop="jsqq" />
      <el-table-column label="技术真实姓名" align="center" prop="jszsxm" />
      <el-table-column label="技术手机号" align="center" prop="jstel" />
      <el-table-column label="包单价格" align="center" prop="bdmoney" />
      <el-table-column label="技术支付宝" align="center" prop="jszfb" />
      <el-table-column label="退单理由" align="center" prop="tddesc">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="showDetail('退单理由', scope.row.tddesc)">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column label="公司内部备注" align="center" prop="nbdesc">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="showDetail('公司内部备注', scope.row.nbdesc)">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column label="售前内部备注" align="center" prop="sqnbbz">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="showDetail('售前内部备注', scope.row.sqnbbz)">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column label="售中内部备注" align="center" prop="sznbbz">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="showDetail('售中内部备注', scope.row.sznbbz)">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column label="付款凭证 多图" align="center" prop="pingzimg">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="showImages('付款凭证', scope.row.pingzimg)">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column label="客服抢单状态" align="center" prop="kfqdlink">
        <template slot-scope="scope">
          <el-tag :type="grabStatusType(scope.row.kfqdlink)" size="mini">
            {{ grabStatusFormat(scope.row.kfqdlink) }}
          </el-tag>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
      small
    />

    <el-dialog :title="detailTitle" :visible.sync="detailDialogVisible" width="400px">
      <div v-if="detailType === 'text'" style="white-space: pre-wrap;word-break: break-all;">{{ detailContent }}</div>
      <div v-else-if="detailType === 'images'">
        <el-image
          v-for="(img, idx) in detailImages"
          :key="idx"
          :src="img"
          style="max-width: 100%; margin-bottom: 8px; display: block;"
          :preview-src-list="detailImages"
        />
        <div v-if="!detailImages.length">无图片</div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="detailDialogVisible = false">关闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { listOrders, getOrders, delOrders, addOrders, updateOrders } from "@/api/dashboard/oldOrders"

export default {
  name: "OldOrders",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      ordersList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        img: null,
        funddate: null,
        cid: null,
        sort: null,
        host: null,
        type: null,
        enddate: null,
        desc: null,
        url: null,
        descs: null,
        uid: null,
        money: null,
        minMoney: null,
        maxMoney: null,
        jmoney: null,
        aid: null,
        did: null,
        shid: null,
        khqq: null,
        khwx: null,
        khtel: null,
        khzfb: null,
        bianhao: null,
        khww: null,
        jsqq: null,
        jszsxm: null,
        jstel: null,
        bdmoney: null,
        jszfb: null,
        tddesc: null,
        zt: null,
        yfmoney: null,
        nbdesc: null,
        zqdate: null,
        sqnbbz: null,
        sznbbz: null,
        tddate: null,
        bddate: null,
        cjdate: null,
        yjdate: null,
        sqbsk: null,
        dingdannum: null,
        szbsk: null,
        kfqdlink: null,
        pingzimg: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      detailDialogVisible: false,
      detailTitle: '',
      detailContent: '',
      detailType: 'text',
      detailImages: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true
      listOrders(this.queryParams).then(response => {
        this.ordersList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        img: null,
        funddate: null,
        cid: null,
        sort: null,
        host: null,
        type: null,
        enddate: null,
        desc: null,
        url: null,
        descs: null,
        uid: null,
        money: null,
        jmoney: null,
        aid: null,
        did: null,
        shid: null,
        khqq: null,
        khwx: null,
        khtel: null,
        khzfb: null,
        bianhao: null,
        khww: null,
        jsqq: null,
        jszsxm: null,
        jstel: null,
        bdmoney: null,
        jszfb: null,
        tddesc: null,
        zt: null,
        yfmoney: null,
        nbdesc: null,
        zqdate: null,
        sqnbbz: null,
        sznbbz: null,
        tddate: null,
        bddate: null,
        cjdate: null,
        yjdate: null,
        sqbsk: null,
        dingdannum: null,
        szbsk: null,
        kfqdlink: null,
        pingzimg: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加【请填写功能名称】"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getOrders(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改【请填写功能名称】"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrders(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addOrders(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除【请填写功能名称】编号为"' + ids + '"的数据项？').then(function() {
        return delOrders(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/orders/export', {
        ...this.queryParams
      }, `orders_${new Date().getTime()}.xlsx`)
    },
    
    // 订单状态显示
    statusFormat(status) {
      switch (status) {
        case 0: return '未接单';
        case 1: return '开发中';
        case 2: return '技术已提交';
        case 3: return '移交客户';
        case 4: return '完成';
        case 5: return '技术退单';
        case 6: return '客户退单';
        case 7: return '问题订单';
        case 8: return '半成品成交';
        default: return '未知';
      }
    },
    // 订单状态样式
    statusType(status) {
      switch (status) {
        case 0: return 'info'; // 未接单
        case 1: return 'warning'; // 开发中
        case 2: return 'primary'; // 技术已提交
        case 3: return 'success'; // 移交客户
        case 4: return 'success'; // 完成
        case 5: return 'danger'; // 技术退单
        case 6: return 'danger'; // 客户退单
        case 7: return 'danger'; // 问题订单
        case 8: return 'primary'; // 半成品成交
        default: return 'info';
      }
    },
    // 抢单状态显示
    grabStatusFormat(status) {
      if (status === 0) return '未抢单'
      if (status === 1) return '已抢单'
      return '未知'
    },
    // 抢单状态样式
    grabStatusType(status) {
      if (status === 0) return 'info'
      if (status === 1) return 'success'
      return 'warning'
    },
    // 加急状态显示
    urgentFormat(status) {
      if (status === 1) return '加急'
      if (status === 0) return '不加急'
      return '未知'
    },
    // 加急状态样式
    urgentType(status) {
      if (status === 1) return 'danger'
      if (status === 0) return ''
      return 'info'
    },
    showDetail(title, content) {
      this.detailTitle = title
      this.detailContent = content || '无内容'
      this.detailType = 'text'
      this.detailDialogVisible = true
    },
    showImages(title, imgs) {
      this.detailTitle = title
      if (imgs) {
        this.detailImages = imgs.split(',').map(i => i.trim()).filter(Boolean)
      } else {
        this.detailImages = []
      }
      this.detailType = 'images'
      this.detailDialogVisible = true
    }
  }
}
</script>

<style scoped>
.compact-container {
  padding: 6px 6px 0 6px;
}
.el-form-item {
  margin-bottom: 4px !important;
}
.el-table th, .el-table td, .el-table__row {
  padding: 2px 2px !important;
  font-size: 12px;
  line-height: 16px !important;
  height: 24px !important;
  max-height: 24px !important;
}
.el-table__body-wrapper {
  max-height: 400px;
  overflow-y: auto;
}
.el-table .cell {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 120px;
}
</style>
